import { Field, Mapping, Property } from "../types";

/**
 * 索引字段映射
 * @param mappings 映射
 * @param version 大版本
 * @returns  字段
 */
export function IndexFieldBuild(
  mappings: Record<string, Mapping> | Mapping,
  version: number
): Array<Field> {
  const fields = new Array<Field>();


  if (version <= 6) {
    // es版本小于6.0.0，则一定有类型
    // 全部类型
    const types = Object.keys(mappings);
    for (const type of types) {
      const mapping = (mappings as Record<string, Mapping>)[type];
      const properties = mapping.properties;
      const prefix = "";
      if (properties) {
        for (const key in properties) {
          buildField(key, properties[key], fields, prefix, type);
        }
      }
    }
  } else if (version >= 8) {
    // 如果es版本大于等于8，则一定没有类型
    const properties = (mappings as Mapping).properties;
    const prefix = "";
    if (properties) {
      for (const key in properties) {
        buildField(key, properties[key], fields, prefix, "");
      }
    }
  } else {
    // es版本是v7，则可能有，也可能没有
    // 情况 1: 直接有 properties → 无 type（v7 默认 / v8）
    if (mappings.properties && typeof mappings.properties === "object") {
      const properties = (mappings as Mapping).properties;
      const prefix = "";
      if (properties) {
        for (const key in properties) {
          buildField(key, properties[key], fields, prefix, "");
        }
      }
    }
    // 情况 2: 顶层是 type 名（如 _doc）→ 有 type（v6 / v7 兼容模式）
    else {
      const topLevelKeys = Object.keys(mappings);
      // 找第一个值包含 properties 的 key（v7 只有一个 type）
      for (const type of topLevelKeys) {
        const typeMapping = (mappings as Record<string, Mapping>)[type];
        if (typeMapping && typeMapping.properties && typeof typeMapping.properties === "object") {
          const properties = typeMapping.properties;
          const prefix = "";
          if (properties) {
            for (const key in properties) {
              buildField(key, properties[key], fields, prefix, type);
            }
          }
        }
      }
    }
  }

  return fields;
}

function buildField(
  name: string,
  field: Property,
  fields: Array<Field>,
  prefix: string,
  _type: string
): void {
  let realName = name;
  let dataIndex = name;
  if (prefix !== "") {
    realName = prefix + "." + name;
    dataIndex = prefix + "-" + name;
  }
  if (field.fields) {
    // 如果存在`fields`字段，则代表这个字段是`text`类型+`keyword`类型
    fields.push({
      name: realName,
      type: field.type || "",
      dataIndex,
      label: _type ? _type + "." + realName : realName
    });
    const keywordName = realName + ".keyword";
    fields.push({
      name: keywordName,
      type: "keyword",
      dataIndex: "",
      label: _type ? _type + "." + keywordName : keywordName
    });
  } else if (field.properties) {
    for (const key in field.properties) {
      buildField(key, field.properties[key], fields, realName, _type);
    }
  } else {
    fields.push({
      name: realName,
      type: field.type || "",
      dataIndex,
      label: _type ? _type + "." + realName : realName
    });
  }
}
